package com.yic.module.base.service.brand;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.base.dal.dataobject.center.CenterDO;
import com.yic.module.base.dal.redis.RedisKeyConstants;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.base.controller.admin.brand.vo.*;
import com.yic.module.base.dal.dataobject.brand.BrandDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.base.convert.brand.BrandConvert;
import com.yic.module.base.dal.mysql.brand.BrandMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.base.enums.ErrorCodeConstants.*;
import static com.yic.module.base.enums.ErrorCodeConstants.BRAND_CODE_EXISTS;

/**
 * 品牌信息 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class BrandServiceImpl implements BrandService {

    @Resource
    private BrandMapper brandMapper;

    @Override
    public Long createBrand(BrandCreateReqVO createReqVO) {
        validateCreateOrUpdate(null, createReqVO.getName(), createReqVO.getCode());
        // 插入
        BrandDO brand = BrandConvert.INSTANCE.convert(createReqVO);
        brandMapper.insert(brand);
        // 返回
        return brand.getId();
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.CENTER, key = "#updateReqVO.id")
    public void updateBrand(BrandUpdateReqVO updateReqVO) {
        // 校验存在
        validateCreateOrUpdate(updateReqVO.getId(), updateReqVO.getName(), updateReqVO.getCode());
        // 更新
        BrandDO updateObj = BrandConvert.INSTANCE.convert(updateReqVO);
        brandMapper.updateById(updateObj);
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.BRAND, key = "#id")
    public void deleteBrand(Long id) {
        // 校验存在
        validateBrandExists(id);
        // 删除
        brandMapper.deleteById(id);
    }

    private void validateCreateOrUpdate(Long id, String name, String code){
        validateNameExists(id, name);
        validateCodeExist(id, code);
    }

    private void validateNameExists(Long id, String name) {
        BrandDO brand = brandMapper.selectByName(name);
        if (ObjectUtil.isEmpty(brand)){
            return;
        }
        if (id == null){
            throw exception(BRAND_NAME_EXISTS);
        }
        if (!id.equals(brand.getId())){
            throw exception(BRAND_NAME_EXISTS);
        }
    }

    private void validateCodeExist(Long id, String code){
        BrandDO brand = brandMapper.selectByCode(code);
        if (ObjectUtil.isEmpty(brand)){
            return;
        }
        if (id == null){
            throw exception(BRAND_CODE_EXISTS);
        }
        if (!id.equals(brand.getId())){
            throw exception(BRAND_CODE_EXISTS);
        }
    }

    private void validateBrandExists(Long id) {
        if (brandMapper.selectById(id) == null) {
            throw exception(BRAND_NOT_EXISTS);
        }
    }

    @Override
    @Cacheable(cacheNames = RedisKeyConstants.BRAND, key = "#id")
    public BrandDO getBrand(Long id) {
        return brandMapper.selectById(id);
    }

    @Override
    public List<BrandDO> getBrandList(Collection<Long> ids) {
        return brandMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BrandDO> getBrandPage(BrandPageReqVO pageReqVO) {
        return brandMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BrandDO> getBrandList(BrandExportReqVO exportReqVO) {
        return brandMapper.selectList(exportReqVO);
    }

    @Override
    public List<BrandDO> getBrandList(Collection<Long> ids, Collection<Integer> statuses) {
        return brandMapper.selectList(ids, statuses);
    }

}
